#!/usr/bin/env python3.9
from PIL import Image, ImageDraw, ImageFont

COLOURSPACE="RGB"
#COLOURSPACE="YCbCr"
CARDSIZE=(320,256)
#CARDSIZE=(640,496)
#CARDSIZE=(800,616)
(xmax, ymax) = CARDSIZE
foreground = (255,255,255)
background = (0,0,0,0)

fnt = ImageFont.truetype("/usr/home/cjb/.fonts/PetMe64.ttf", 16)
im = Image.new(mode=COLOURSPACE, size=CARDSIZE, color=background)
d = ImageDraw.Draw(im)


def docheck (xs, ys, col1, col2, pixsize):
  global si, d
  s = si // pixsize
  y = int(ys) -1
  if s == 0:
      return

  while (y-ys) <= si:
    x = int(xs) -1
    cp = (int(y - ys) // s) & 1

    while (x-xs) <= si:
      c = (int(x - xs) // s) & 1
      if (c ^ cp):
          im.putpixel ((x, y), col1)
      else:
          im.putpixel ((x, y), col2)

      x += 1
    y +=1


gx = xmax // 32
gy = ymax // 32
si = min (gx, gy) * 4

yp = gy
p = 0
while (yp <= (28*gy)):
  xp = gx
  c1 = ((255,255,255), (255,0,0), (255,0,0), (255,0,0),     (255,0,0))
  c2 = ((0,0,0),       (0,0,0), (0,0,255),   (255,255,255), (128,128,128))
  try:
      col1 = c1[p]
      col2 = c2[p]
  except:
      col1 = (0,0,0)
      col2 = (255,255,0)
  p += 1

  pp = 1
  while (xp <= (28*gx)):
    ps = xmax // 16 // pp #ps // 2
    docheck (xp, yp, col1, col2, ps)
    xp += 3.5*gx
    pp += 1
  yp += 5*gy


d.text ((-1+xmax/2,3), "VK2CJB", font=fnt, fill=(0,0,0))
d.text ((1+xmax/2,5), "VK2CJB", font=fnt, fill=(255,255,255))
d.text ((xmax/2,4), "VK2CJB", font=fnt, fill=(160,160,160))
imrgb = im.convert("RGB")
s = "colourchecks-" + str(xmax) + "x" + str(ymax) + ".png"
print (s)
imrgb.save(s)
